package com.schedule.menu.controller;


import com.schedule.common.CommonResult;
import com.schedule.menu.entity.Menu;
import com.schedule.menu.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    //新增
    @PostMapping
    public CommonResult<String> add(@RequestBody Menu menu) {
        menu.setCreateTime(new Date());
        menu.setUpdateTime(new Date());
        boolean save = menuService.save(menu);
        if (save) {
            return CommonResult.success("新增成功!");
        }
        return CommonResult.error("新增失败!");
    }

    //编辑
    @PutMapping
    public CommonResult<String> edit(@RequestBody Menu menu) {
        menu.setUpdateTime(new Date());
        boolean save = menuService.updateById(menu);
        if (save) {
            return CommonResult.success("编辑成功!");
        }
        return CommonResult.error("编辑失败!");
    }

    //删除
    @DeleteMapping("/{menuId}")
    public CommonResult<String> deleteMenu(@PathVariable("menuId") Long menuId) {
        menuService.deleteMenu(menuId);
        return CommonResult.success("删除成功!");
    } 

    //列表
    @GetMapping("/list")
    public CommonResult<List<Menu>> getList(){
        List<Menu> list = menuService.getList();
        return CommonResult.success("查询成功",list);
    }

    //上级菜单树数据
    @GetMapping("/parent")
    public CommonResult< List<Menu>> getParentList(){
        List<Menu> list = menuService.parentList();
        return CommonResult.success("查询成功",list);
    }
}
